#include <QString>
#include <DebugFunction.h>
#include <mainwindow.h>
#include <ui_mainwindow.h>
#include <QMessageBox>
void MainWindow::ReadDebugConfig()
{
    QFile file;
    file.setFileName("调试界面配置.ini");
    int atLine=0;
    buttonGroup.oneButtonCount=atLine;
    if(file.open(QFile::ReadOnly))
    {
        while (!file.atEnd())
        {
            QByteArray readArray=file.readLine();
            QString readStr=readArray.toStdString().c_str();
            atLine++;

            buttonGroup.oneButtonCount=atLine;


            QStringList tmp;
            tmp=readStr.split(",");
            GetDebugConfigContent(atLine-1,tmp);

            if(atLine>=288)
                return;
        }
        file.close();
    }

}
void MainWindow::GetDebugConfigContent(int rows,QStringList list)
{
    for(int i=0;i<=list.count()-1;i++)
    {
        if(list[i].contains("同步按钮"))
        {
            QString tmp;
            tmp=list[i].remove(0,list[i].indexOf('=')+1);
            buttonGroup.button[rows].syncButton=tmp.toUInt();
        }
        else if(list[i].contains("按钮"))
        {
            QString tmp;
            tmp=list[i].remove(0,list[i].indexOf('=')+1);
            buttonGroup.button[rows].buttonName=tmp;
            QPushButton *rowButton=GetPushButtonByRowIndex(100+rows+1);
            if(rowButton!=nullptr)
                rowButton->setText(tmp);
        }
        else if(list[i].contains("COM"))
        {
            QString tmp;
            tmp=list[i].remove(0,list[i].indexOf('=')+1);
            buttonGroup.button[rows].comIndex=tmp.toUInt();
        }
        else if(list[i].contains("延时"))
        {
            QString tmp;
            tmp=list[i].remove(0,list[i].indexOf('=')+1);
            tmp=tmp.remove("ms");
            buttonGroup.button[rows].recvDelay=tmp.toUInt();

        }
        else if(list[i].contains("CRC"))
        {
            QString tmp;
            tmp=list[i].remove(0,list[i].indexOf('=')+1);
            if(tmp=="YES")
                buttonGroup.button[rows].crc=true;
            else
                buttonGroup.button[rows].crc=false;
        }
        else if(list[i].contains("TX"))
        {
            QString tmp;
            tmp=list[i].remove(0,list[i].indexOf('=')+1);
            tmp.remove(" ");
            buttonGroup.button[rows].sendFrame=QByteArray::fromHex(tmp.toUtf8());
        }

    }
    qDebug()<<"读取到的调试界面配置文件,按钮名="<<buttonGroup.button[rows].buttonName<<"com口="<<buttonGroup.button[rows].comIndex<<"同步于"<<buttonGroup.button[rows].syncButton;
    qDebug()<<"读取到的调试界面配置文件，延时"<<buttonGroup.button[rows].recvDelay<<"CRC"<<buttonGroup.button[rows].crc<<"tx"<<buttonGroup.button[rows].sendFrame.toHex();

}

Device* MainWindow::GetDevice(int com)
{
    if(com==1)
    {
        return &device1;
    }
    else if(com==2)
    {
        return &device2;
    }
    else if(com==3)
    {
        return &device3;
    }
    else if(com==4)
    {
        return &device4;
    }
    return &device1;
}

void MainWindow::HandleButton(int index,int16_t value)//处理第i个按钮的操作
{
    qDebug()<<__FUNCTION__;
    Device *devicePtr=GetDevice(buttonGroup.button[index].comIndex);
    QByteArray sendData;
    sendData.clear();
    sendData=buttonGroup.button[index].sendFrame;
    if(buttonGroup.button[index].buttonName=="校时")
    {
        if(sendData.length()>7)
        {
            sendData.remove(7,sendData.length()-7);
        }
        sendData.data()[6]=14;


        QByteArray year,month,day,week,hour,minute,second;
        year.resize(2);
        month.resize(2);
        day.resize(2);
        week.resize(2);
        hour.resize(2);
        minute.resize(2);
        second.resize(2);
        qDebug()<<u8"校时组包:"<<sendData.toHex();

        QDateTime CurrentDateTime=QDateTime::currentDateTime();
        uint16_t tmp;
        tmp=CurrentDateTime.date().year();
        year[0]=((tmp&0xff00)>>8);
        year[1]=tmp&0xff;
        sendData.append(year);

        tmp=CurrentDateTime.date().month();
        month[0]=((tmp&0xff00)>>8);
        month[1]=tmp&0xff;
        sendData.append(month);

        tmp=CurrentDateTime.date().day();
        day[0]=((tmp&0xff00)>>8);
        day[1]=tmp&0xff;
        sendData.append(day);

        tmp=CurrentDateTime.date().dayOfWeek();
        week[0]=((tmp&0xff00)>>8);
        week[1]=tmp&0xff;
        sendData.append(week);
        qDebug()<<"周几："<<tmp;

        tmp=CurrentDateTime.time().hour();
        hour[0]=((tmp&0xff00)>>8);
        hour[1]=tmp&0xff;
        sendData.append(hour);

        tmp=CurrentDateTime.time().minute();
        minute[0]=((tmp&0xff00)>>8);
        minute[1]=tmp&0xff;
        sendData.append(minute);

        tmp=CurrentDateTime.time().second();
        second[0]=((tmp&0xff00)>>8);
        second[1]=tmp&0xff;
        sendData.append(minute);
    }
    else if((uchar)sendData[1]==0x06)
    {
        qDebug()<<"button with 06 function";
        if(sendData.length()>=6)
        {
            sendData[4]=((value>>8)&0xff);
            sendData[5]=(value&0xff);
        }
    }

    if(buttonGroup.button[index].crc==true)
    {

        QByteArray data;
        data.resize(2);
        sendData.append(data);
        devicePtr->modbus->GetCRC(sendData,data);
        sendData[sendData.length()-2]=data[0];
        sendData[sendData.length()-1]=data[1];
    }

    qDebug()<<"HandleButton:"<<sendData.toHex();
    buttonGroup.lastSyncButtonDelay=buttonGroup.button[index].recvDelay;
    emit devicePtr->To_WriteData(sendData,buttonGroup.button[index].recvDelay);

}

void MainWindow::HandleSyncButton(int buttonIndex)
{
    //处理同步按钮
    if(buttonGroup.oneButtonCount<=32)
        return;
    else
    {
        for(int i=32;i<=buttonGroup.oneButtonCount-1;i++)
        {
            if(buttonGroup.button[i].syncButton==buttonIndex+1)
            {
                Device *devicePtr=GetDevice(buttonGroup.button[i].comIndex);
                QByteArray sendData=buttonGroup.button[i].sendFrame;
                if(buttonGroup.button[i].crc==true)
                {
                    QApplication::processEvents();
                    QThread::msleep(buttonGroup.lastSyncButtonDelay+500);
                    QApplication::processEvents();

                    QByteArray data;
                    data.resize(2);
                    sendData.append(data);
                    devicePtr->modbus->GetCRC(sendData,data);
                    sendData[sendData.length()-2]=data[0];
                    sendData[sendData.length()-1]=data[1];
                }
                emit devicePtr->To_WriteData(sendData,buttonGroup.button[i].recvDelay);
                buttonGroup.lastSyncButtonDelay=buttonGroup.button[i].recvDelay;
                qDebug()<<u8"同步按钮发送"<<sendData.toHex();
            }
        }
    }
}

QPushButton *MainWindow::GetPushButtonByRowIndex(int RowsIndex)
{
    QString buttonName=QString("pushButton_%0").arg(RowsIndex);
    QPushButton *buttonPtr=nullptr;
    buttonPtr=this->findChild<QPushButton*>(buttonName);
    return buttonPtr;
}

void MainWindow::StopRead()
{
    if(device->timer->isActive())
    {
        timerIsStart=true;
        device->StopTimer();
    }
    else
    {
        timerIsStart=false;
    }

}

void MainWindow::StartRead()
{
    if(timerIsStart)
        device->StartTimer();
}


void MainWindow::ShowRecvData(QByteArray recvData)
{
    ui->textEdit->append(recvData.toHex());
}

void MainWindow::OpenPort()
{
    emit device1.OpenDevice();
    if(device1.modbus->port.isOpen())
    {
        QMessageBox a;
        a.setText("1号串口打开成功");
        a.show();
        a.exec();
    }
    else
    {
        QMessageBox a;
        a.setText("1号串口打开失败");
        a.show();
        a.exec();
    }
}

void MainWindow::OpenPort2()
{
    emit device2.OpenDevice();
    if(device2.modbus->port.isOpen())
    {
        QMessageBox a;
        a.setText("2号串口打开成功");
        a.show();
        a.exec();
    }
    else
    {
        QMessageBox a;
        a.setText("2号串口打开失败");
        a.show();
        a.exec();
    }
}

void MainWindow::OpenPort3()
{
    emit device3.OpenDevice();
    if(device3.modbus->port.isOpen())
    {
        QMessageBox a;
        a.setText("3号串口打开成功");
        a.show();
        a.exec();
    }
    else
    {
        QMessageBox a;
        a.setText("3号串口打开失败");
        a.show();
        a.exec();
    }
}

void MainWindow::OpenPort4()
{
    emit device4.OpenDevice();
    if(device4.modbus->port.isOpen())
    {
        QMessageBox a;
        a.setText("4号串口打开成功");
        a.show();
        a.exec();
    }
    else
    {
        QMessageBox a;
        a.setText("4号串口打开失败");
        a.show();
        a.exec();
    }
}
